package com.usefullc.crawler.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.usefullc.common.exception.BizException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

public class ExcelGenerator {
    public static String write(String templateName, String fileBasePath, String headers, List<List<String>> dataList) {
        try {
            String[] headerArray = headers.split(",");
            for (int i = 0; i < headerArray.length; i++) {
                headerArray[i] = headerArray[i].trim();
            }
//            JSONArray jsonArray = new JSONArray(json);

            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("new");

            // Create header row
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < headerArray.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headerArray[i]);
            }

            // Create data rows
            for (int i = 0; i < dataList.size(); i++) {
                Row dataRow = sheet.createRow(i + 1);
                List<String> itemList = dataList.get(i);

                for (int j = 0; j < itemList.size(); j++) {
                    Cell cell = dataRow.createCell(j);
                    cell.setCellValue(itemList.get(j));
                }
            }
            String filePath = fileBasePath+ File.separator+templateName+"-"+System.currentTimeMillis()+".xlsx";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            return filePath;

        } catch (Exception e) {
            throw new BizException(e);
        }
    }

    public static String write(String templateName,String fileBasePath,String headers,String jsonBody) {
        try {

            String[] headerArray = headers.split(",");
            for (int i = 0; i < headerArray.length; i++) {
                headerArray[i] = headerArray[i].trim();
            }
//            JSONArray jsonArray = new JSONArray(json);

            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet("new");

            // Create header row
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < headerArray.length; i++) {
                Cell cell = headerRow.createCell(i);
                cell.setCellValue(headerArray[i]);
            }

            // Create data rows
            JSONArray jsonArray = JSONObject.parseArray(jsonBody);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Row dataRow = sheet.createRow(i + 1);

                for (int j = 0; j < headerArray.length; j++) {
                    Cell cell = dataRow.createCell(j);
                    String key = headerArray[j];
                    if (jsonObject.containsKey(key)) {
                        cell.setCellValue(jsonObject.getString(key));
                    }
                }
            }

            String filePath = fileBasePath+ File.separator+templateName+"-"+System.currentTimeMillis()+".xlsx";
            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            return filePath;

        } catch (Exception e) {
            throw new BizException(e);
        }
    }

    public static boolean writeAppend(String filePath,String jsonBody) {
        try {

            FileInputStream fis = new FileInputStream(filePath);
            Workbook workbook = new XSSFWorkbook(fis);
            Sheet sheet = workbook.getSheet("new");

            JSONArray jsonArray = JSONObject.parseArray(jsonBody);
            int lastRowNum = sheet.getLastRowNum();

            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Row dataRow = sheet.createRow(lastRowNum + i + 1);

                for (int j = 0; j < sheet.getRow(0).getLastCellNum(); j++) {
                    Cell cell = dataRow.createCell(j);
                    String key = sheet.getRow(0).getCell(j).getStringCellValue();
                    if (jsonObject.containsKey(key)) {
                        cell.setCellValue(jsonObject.getString(key));
                    }
                }
            }

            fis.close();

            FileOutputStream fileOut = new FileOutputStream(filePath);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            return true;

        } catch (Exception e) {
            throw new BizException(e);
        }
    }
}
